package com.zeroes.cons;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.zeroes.common_prelude.*;

import android.util.Log;

public class ZSGroup {
	
	public String id="";
	public String parentID="";
	public ZSLayer layer;
	public List<ZSSprite> Content=null;
	public boolean transformDirty=false;
	public boolean visible=true;
	private Quad bbox;
	public float rotation=0;
	public float gsx=1;
	public float gsy=1;
	public float gcx;
	public float gcy;
	public int width;
	public int height;
	public float tx=0;
	public float ty=0;
	public float anchorX=0.5f;
	public float anchorY=0.5f;
	
	public float opacity=1;
	
	public ZSGroup(String title,ZSLayer parent){
		
	this.id=title;
	this.parentID=parent.title;
	this.layer=parent;
	transformDirty=true;
	this.bbox=new Quad();
	
	}
	
	public void visit(float a){
		
		if(layer.SocialDirty){
			
			Content=layer.getSpritesinGroup(id);
			if(layer.Groups.indexOf(this)==layer.Groups.size()-1){
				
				layer.SocialDirty=false;
			}
			//this.calculateBB();
		}
		
		transform();
		transformDirty=false;
		
	}
	
	private void transform(){
		if(transformDirty&&visible){
			this.calculateBB();
			
		//	Log.v("gtrans", this.id+" cx , cy >>>"+gcx+" , "+gcy+" content size "+Content.size() );
		}
		
	}
	
	public void setOpacity(float o){
		if(o>1){
			this.opacity=1;
		}
		else{
			if(o<0) 
				this.opacity=0;
			else
				this.opacity=o;
		}
		if(Content!=null){

			for(ZSSprite s:Content){
				s.opacity=this.opacity;
		
			}
			}
		
	}
	
	public void translate(float _tx,float _ty){
		this.tx=_tx*this.layer.thisGame.getXRatio();
		this.ty=_ty*this.layer.thisGame.getYRatio();
		this.markTransformDirty();
		
	}
	public void rotateCounterClockwise(float deg){
		this.rotation-=deg;
		
		if(this.rotation<=-360)
		{
			this.rotation+=360;
			
		}
		this.markTransformDirty();
		
	}
	public void setScale(float onX, float onY){
		
		this.gsx=onX;
		this.gsy=onY;
		this.markTransformDirty();
	}
	
	public void setGroupRotation(int r){
		this.rotation=r;
		this.markTransformDirty();
		
	}
	
	public void hide(){
		
		if(Content!=null){
		this.visible=false;
			for(ZSSprite s:Content){
				s.hide();
		
			}
			}
	}
	
	public void show(){
		
		if(Content!=null){
		this.visible=true;
			for(ZSSprite s:Content){
				s.show();
		
			}
			}
	}
	
	
	public void rotateClockwise(float deg){
		this.rotation+=deg;
		
		if(this.rotation>=360)
		{
			this.rotation-=360;
			//this.p_rotation-=360;
		}
		this.markTransformDirty();
		
	}
	public void setRotation(float rot){
		this.rotation=rot;
		this.markTransformDirty();
	}
public void calculateBB(){
	float minleft=0,mintop=0,maxright=0,maxbottom=0;
	Rect sbb;
	int idx=0;
	for(ZSSprite s:Content){
		
		sbb=s.getBBox();
		if(idx==0){
			minleft=sbb.left;
			mintop=sbb.top;
			maxright=sbb.right;
			maxbottom=sbb.bottom;
		}
		else
		{
			minleft=(sbb.left<minleft) ? sbb.left : minleft; 
			mintop=(sbb.top<mintop) ? sbb.top : mintop;
			maxright=(sbb.right>maxright) ? sbb.right : maxright;
			maxbottom=(sbb.bottom>maxbottom) ? sbb.bottom : maxbottom;
			
			
		}
		
		idx++;
	}
	
	this.bbox.set_from_rect(new Rect(minleft,mintop,maxright,maxbottom));
	this.gcx=minleft+((maxright-minleft)*anchorX);
	this.gcy=mintop+((maxbottom-mintop)*anchorY);
	
}

public void markTransformDirty(){
	
	this.transformDirty=true;
	if(Content!=null){
	for(ZSSprite s:Content){
		s.marktransformDirty();
	//	s.marktransformGroupDirty();
	}
	}
}

}
